<?php

namespace App\Http\Controllers;

use App\Models\Coin;
use App\Models\SignConfig;
use App\Models\User;
use App\Models\UserAmountLog;
use App\Models\UserSign;
use App\Models\UserSignLog;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;

class SignController extends Controller
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function config(Request $request)
    {
        $list = SignConfig::query()->get()->toArray();
        return $this->success($list);
    }

    public function userSign(Request $request)
    {
        $user = $request->user()->toArray();

        $config = SignConfig::query()->get()->keyBy('day')->toArray();

        $info = UserSign::query()->where('user_id', $user['id'])->first();
        $info = $info ? $info->toArray() : [];
        if ($info && date('Y-m-d', strtotime($info['last_sign'])) == date('Y-m-d')) {
            return $this->error('今天已经签到过了');
        }

        $series_days = $info ? $info['series_days'] + 1 : 1;
        $continuity_days = 1;
        $sign_reward = $config[$continuity_days]['number'];

        // 连续签到
        if ($info && date('Y-m-d', strtotime($info['last_sign']) + 86400) == date('Y-m-d')) {
            $continuity_days = $info['continuity_days'] + 1;
            $sign_reward = $config[$continuity_days]['number'];
        }

        UserSign::query()->where('user_id', $user['id'])->updateOrCreate(['user_id' => $user['id']], [
            'series_days' => $series_days,
            'continuity_days' => $continuity_days,
            'last_sign' => date('Y-m-d H:i:s'),
        ]);
        $log = UserSignLog::query()->create([
            'user_id' => $user['id'],
            'sign_reward' => $sign_reward,
            'sign_time' => date('Y-m-d H:i:s'),
        ]);
        User::query()->where('id', $user['id'])->increment('amount', $sign_reward);
        $user = User::query()->where('id', $user['id'])->first();
        UserAmountLog::query()->create(
            [
                'user_id' => $user->id,
                'amount' => $sign_reward,
                'balance' => $user->amount,
                'type' => 0,
                'map_id' => $log->id,
                'remark' => '每日签到',
            ]
        );

        return $this->success();
    }

    public function userSignInfo(Request $request)
    {
        $user = $request->user()->toArray();
        $log = UserSign::query()->where('user_id', $user['id'])->first();
        if (!$log) {
            return $this->success();
        }

        if (strtotime(date('Y-m-d', strtotime($log->last_sign))) < strtotime(date('Y-m-d', strtotime('-1 days')))) {
            $log->continuity_days = 0;
            $log->save();
        } else if ($log->continuity_days == 7 && strtotime(date('Y-m-d', strtotime($log->last_sign))) == strtotime(date('Y-m-d', strtotime('-1 days')))) {
            $log->continuity_days = 0;
            $log->save();
        }
        $log->is_sign = date('Y-m-d', strtotime($log->last_sign)) == date('Y-m-d');
        return $this->success($log->toArray());
    }
}
